package com.ncloud.crm.service.impl;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.crm.common.Constant;
import com.ncloud.crm.entity.order.WorkNodeCloudIpseg;
import com.ncloud.crm.mapper.WorkPublicCloudIpsegMapper;
import com.ncloud.crm.service.WorkNodePublicCloudSegService;
import com.ncloud.crm.utils.DateUtils;
import com.ncloud.crm.utils.TableIdDefineUtils;
import com.ncloud.param.service.SequenceService;

/**
 * Created by shiqiang on 2017/4/17.
 */
@Service
public class WorkNodePublicCloudSegServiceImpl implements WorkNodePublicCloudSegService {
    private static final Logger logger = LogManager.getLogger(WorkNodePublicCloudSegServiceImpl.class);
    @Autowired
    private WorkPublicCloudIpsegMapper workPublicCloudIpsegMapper;
    @Reference
    private SequenceService sequenceService;
    @Override
    public WorkNodeCloudIpseg getPk(Long id) {
        return workPublicCloudIpsegMapper.getWorkCloudSegByPk(id);
    }

    @Override
    public int insert(JSONObject json) {
        WorkNodeCloudIpseg seg = JSON.parseObject(json.toJSONString(),WorkNodeCloudIpseg.class);
        if(seg.getSegInstId() ==null){
            Long id = sequenceService.getSequence("seq_cloud_seg_id");
            seg.setSegInstId(id);
        }
        return workPublicCloudIpsegMapper.insertWorkCloudSeg(seg);
    }

    @Override
    public JSONArray validate(JSONObject cloud, JSONObject result) {
        JSONArray returnSeg = new JSONArray();
        if(cloud == null){
            throw new UserException(ResultEnum.PARAM_ERROR);
        }
        if(cloud.containsKey("ipseg")){
            String modifyTag = cloud.getString("modifyTag");
            JSONArray segArray = cloud.getJSONArray("ipseg");
            WorkNodeCloudIpseg seg = null;
            for(int i=0;i<segArray.size();i++){
                JSONObject segJson = segArray.getJSONObject(i);
                seg = JSON.parseObject(segJson.toJSONString(),WorkNodeCloudIpseg.class);
                seg.setModifyTag(modifyTag);
                seg.setWorkId(result.getLong(TableIdDefineUtils.WORK_ID));
                if(cloud.containsKey(TableIdDefineUtils.CLOUD_ID)){
                    seg.setCloudId(cloud.getLong(TableIdDefineUtils.CLOUD_ID));
                }else{
                    seg.setCloudId(result.getLong(TableIdDefineUtils.CLOUD_ID));
                }
                seg.setUpdateTime(DateUtils.parseDate(result.getString("createDate")));
                seg.setUpdateUser(result.getLong("userID"));
                if(Constant.ADD.equals(modifyTag)){
                    seg.setBeginDate(DateUtils.parseDate(result.getString("createDate")));
                    seg.setEndDate(DateUtils.parseDate(Constant.END_DATE));

                }else if(Constant.UPDATE.equals(modifyTag)){

                    //变更暂时不考虑
                    //seg.setModifyTag(Constant.ADD);
                }else if(Constant.DEL.equals(modifyTag)){
                    seg.setEndDate(DateUtils.parseDate(result.getString("createDate")));

                }else{

                }
                /*保存信息*/
                returnSeg.add(JSON.parseObject(JSON.toJSONString(seg)));
            }

        }
        return returnSeg;
    }

    @Override
    public List<WorkNodeCloudIpseg> getWorkCloudSegByWorkID(Long wrokId) {
        return workPublicCloudIpsegMapper.getWorkCloudSegByWorkId(wrokId);
    }
    
    @Override
	public List<WorkNodeCloudIpseg> getNewIpSegsByCloudId(Long workId,Long cloudId) {
		return workPublicCloudIpsegMapper.getNewIpSegsByCloudId(workId,cloudId);
	}
}
